import ItemComponent from './itemComponent'
import { totalConfig, propName } from 'config/templateConfig'
let itemConfig = totalConfig.item
const MAX_STACK = 999
class PackageComponent {
    constructor(opts) {
        opts = opts || {}
        this.size = 40; //背包大小
        this.packageType = opts.packageType || 1 //包裹类型
        this.items = opts.items || new Array(this.size) //背包数组 [itemComponent1,itemComponent2,....]
        this.itemsById = {} //物品列表集合，键为itemId=>[itemComponent1,itemComponent2]
        this.emptySize = this.size;
    }

    //初始化背包
    initPackag() {
        this.emptySize = this.size
        for (let i in this.items) {
            let itemComponent = this.items[i]
            if (!itemComponent) {
                continue
            }
            let templateId = itemComponent.templateId
            itemComponent = ItemComponent.getPropComponent(itemComponent)
            this.items[i] = itemComponent
            itemComponent.pos = Number(i)
            if (!this.itemsById[templateId]) {
                this.itemsById[templateId] = []
            }
            this.itemsById[templateId].push(itemComponent)
            this.emptySize -= 1 //空格子数量
        }
    }

    //获取背包信息
    getInfo() {
        return {
            items: this.items,
            size: this.size,
            emptySize: this.emptySize
        }
    }

    //获取背包大小
    getSize() {
        return this.size
    }

    //获取指定位置的物品以及物品的位置信息
    getItemByPos(pos) {
        return this.items[pos]
    }

    //添加物品
    addItems(items) {
        let size = items.length
        if (this.emptySize < size) {
            return -1 //背包空间不足
        }
        for (let i = 0; i < size; i++) {
            let item = items[i]
            let itemComponent = ItemComponent.getPropComponent({
                templateId: item[1],
                stack: item[0]
            })
            this.addItem(itemComponent)
        }
        return 0
    }

    //添加物品
    addItem(item) {
        if(!itemConfig[item[0]]){
            console.log(`${item[0]}该物品不存在`)
            return 
        }
        let itemComponent = ItemComponent.getPropComponent({
            templateId: item[0],
            stack: item[1]
        })
        return this.addItemComponent(itemComponent)
    }

    //添加物品
    addItemComponent(itemComponent) {
        //判断背包是否有空
        if (this.emptySize === 0) {
            return -1
        }
        //判断物品是否可以叠加
        let itemMaxStack = itemConfig[itemComponent.templateId].maxStack
        if (itemMaxStack) {
            //不可叠加，添加到空格子
            this._addItemToPosition(itemComponent)
            return 0
        }
        //查找物品是否已经存在
        let itemInfoId = this.itemsById[itemComponent.templateId]
        if (!itemInfoId) {
            //不存在，添加到空格子
            this._addItemToPosition(itemComponent)
        } else {
            let num = itemComponent.stack //叠加数
            for (let i in itemInfoId) {
                let p = itemInfoId[i]
                let stack = p.stack
                if (itemComponent.bind === p.bind) { //是否都是绑定或不绑定
                    if (stack + num <= MAX_STACK) {
                        p.stack = stack + num
                        num = 0
                        return 0
                    } else {
                        num = stack + num - MAX_STACK
                        p.stack = MAX_STACK
                    }
                }
            }
            if (num > 0) {
                //剩余的数量添加到空格子
                itemComponent.stack = num
                this._addItemToPosition(itemComponent)
            }
        }
        return 0
    }

    //添加物品到空格子
    _addItemToPosition(itemComponent) {
        let pos = this._getEmpty() //获取空格子的下标
        if (pos === -1) {
            return -1
        }
        itemComponent.pos = pos
        this.emptySize -= 1 //空格子数量减一
        this.items[pos] = itemComponent
        let itemInfoId = this.itemsById[itemComponent.templateId]
            //查找物品是否已经存在
        if (itemInfoId) {
            itemInfoId.push(itemComponent)
        } else {
            this.itemsById[itemComponent.templateId] = [itemComponent]
        }
    }

    //获取一个空格子的下标
    _getEmpty() {
        if (this.emptySize === 0) {
            return -1
        }
        for (let i = 0; i < this.size; i++) {
            if (!this.items[i]) {
                return i
            }
        }
    }

    //交换物品的位置
    transPositionItems(pos, topos) {
        let fromItemComponent = this.items[pos]
        let toItemComponent = this.items[topos]
        this.items[pos] = toItemComponent
        this.items[topos] = fromItemComponent
        toItemComponent.pos = topos
        fromItemComponent.pos = pos
        let fromItemInfoById = this.itemsById[fromItemComponent.templateId]
            /* fromItemInfoById.sort(function(itemComponent1, itemComponent2) { //排序
                return itemComponent1.pos > itemComponent2.pos ? 1 : -1
              })
              let toItemInfoById = this.itemsById[toItemComponent.templateId]
              toItemInfoId.sort(function(itemComponent1, itemComponent2) { //排序
                return itemComponent1.pos > itemComponent2.pos ? 1 : -1
              })*/
    }

    //移除包裹中的物品
    removeItemByItemId(itemId, num) {
        let itemInfoById = this.itemsById[itemId]
        for (let i = itemInfoById.length - 1; i >= 0; i--) {
            let p = itemInfoById[i]
            let stack = p.stack
            if (stack > num) {
                p.stack -= num
                return 0
            } else if (stack === num) {
                this.removeItemByPosition(p.pos)
                return 0
            } else {
                num -= p.stack
                this.removeItemByPosition(p.pos)
            }
        }
        return 0
    }

    //根据下标删除背包中的物品
    removeItemByPosition(pos) {
        let item = this.items[pos]
        if (!item) {
            return 1
        }
        this.emptySize += 1 //空格子加一
        let itemId = item.templateId
        this.items[pos] = null

        let itemInfoById = this.itemsById[itemId]
        if (itemInfoById.length === 1) {
            delete this.itemsById[itemId]
        } else {
            for (let i in itemInfoById) {
                if (itemInfoById[i].pos === pos) {
                    itemInfoById.splice(i, 1)
                    break
                }
            }
        }
        return 0
    }

    removeSomeItemByPosition(pos, num) {
        if (this.items[pos].stack === num) {
            this.removeItemByPosition(pos)
        } else if (this.items[pos].stack < num) {
            return 1
        } else {
            this.items[pos].stack -= num
        }
        return 0
    }

    //获取某一物品的总数量
    getTotalNumByItemId(itemId) {
        let itemInfoId = this.itemsById[itemId]
            //查找物品是否已经存在
        if (!itemInfoId) {
            return 0
        }

        let total = 0
        for (let p of itemInfoId) {
            total += p.stack
        }
        return total
    }

    //合并物品
    mergeItems(frompos, topos) {
        if (frompos === topos) {
            return 1 //位置一致，不能合并
        }
        let fromItemComponent = this.items[frompos]
        let toItemComponent = this.items[topos]
        if (!fromItemComponent || !toItemComponent) {
            return 1 //有物品不存在不能合并
        }

        if (fromItemComponent.templateId !== toItemComponent.templateId) {
            return 1 //物品类型不同不能合并
        }

        if (fromItemComponent.bind !== toItemComponent.bind) {
            return 1 //物品类型交易类型不一样
        }

        let maxStack = itemConfig[fromItemComponent.templateId].maxStack || MAX_STACK
        let fromstack = fromItemComponent.stack
        let tostack = toItemComponent.stack
        if (fromstack === maxStack) {
            return 0 //起始位置物品堆叠数达到上限，不能进行合并
        }

        if (tostack === maxStack) {
            //置换
            this.transPositionItems(frompos, topos)
            return 0
        }

        if (tostack + fromstack < maxStack) {
            fromItemComponent.stack = tostack + fromstack
            this.removeItemByPosition(topos)
        } else {
            let startstack = maxStack
            let endstack = tostack + fromstack - maxStack
            fromItemComponent.stack = startstack
            toItemComponent.stack = endstack
        }
    }

    //合并包裹中能合并的物品
    mergePackage() {
        for (let i = 0; i < this.size - 1; i++) {
            for (let j = i + 1; j < this.size; j++) {
                this.mergeItems(i, j)
            }
        }
    }

    //清空包裹中的物品
    clearPackage() {
        this.itemsById = {}
        this.items = []
        this.emptySize = this.size
    }

    //包裹整理
    arrange() {
        this.mergePackage() //先合并所有的物品
            //items排序
        this.items.sort(function (item1, item2) {
            if (item1 && item2) {
                if (item1.templateId === item2.templateId) {
                    return item1.stack >= item2.stack ? 1 : -1
                } else {
                    return item1.templateId > item2.templateId ? 1 : -1
                }
            } else if (!item1 && item2) {
                return 1
            } else if (item1 && !item2) {
                return -1
            } else {
                return 1
            }
        })
        this.initPackag()
    }
}

module.exports = PackageComponent